package jzqb.service.impl;


import haha.life.normal.RandomUtils;
import haha.life.normal.TokenUtils;
import jzqb.dao.MonitorDao;
import jzqb.po.MonitorPo;
import jzqb.service.MonitorService;
import java.lang.reflect.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


@Service
public class MonitorServiceImpl implements MonitorService {

    @Autowired
    private MonitorDao monitorDao;;


    @Override
    public Boolean record(MonitorPo po) {
        String nowStr = RandomUtils.nowStr("");
        po.setTime(nowStr);
        return monitorDao.record(po);
    }

    @Override
    public Boolean record(HttpServletRequest request, String operateType, String...args) throws IllegalAccessException, NoSuchFieldException {
        String userID = TokenUtils.getCurrentUserId(request);
        return record(userID,operateType,args);
    }

    @Override
    public Boolean record(String userid, String operateType, String...args) throws IllegalAccessException, NoSuchFieldException {
        MonitorPo po = new MonitorPo();
        //操作人
        po.setUserid(userid);
        //操作类型
        po.setOperateType(operateType);
        //操作时间
        String nowStr = RandomUtils.nowStr("");
        po.setTime(nowStr);
        //自定义参数
        for (int i = 0; i < args.length; i++) {
            String attrName = "arg"+ i;
            Field field = po.getClass().getDeclaredField(attrName);
            field.setAccessible(true);
            field.set(po,args[i]);
        }

        return monitorDao.record(po);
    }
}
